package net.tuxun.customer.module.MapDB.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import net.tuxun.customer.module.MapDB.service.IOperateMapDBService;
import org.mapdb.BTreeMap;
import org.mapdb.DB;

public class OperateMapDBServiceImpl implements IOperateMapDBService{
	
	private DB database;
	private BTreeMap<String, HashSet<String>> monitorDataMap;  
	 
	//初始化map
	public void init() throws Exception {  
	       monitorDataMap = database.getTreeMap("monitorDataMap");  
	       if(!monitorDataMap.containsKey("preSubmitKey")){
	    	  
	    	   monitorDataMap.put("preSubmitKey",new HashSet<String>());
	       }
	       if(!monitorDataMap.containsKey("submitKey")){//提交后返回的key
	    	   monitorDataMap.put("submitKey",new HashSet<String>());
	       }
	       if(!monitorDataMap.containsKey("parseSuccessKey")){
	    	   monitorDataMap.put("parseSuccessKey",new HashSet<String>());
	       }
	       if(!monitorDataMap.containsKey("parseFailureKey")){
	    	   monitorDataMap.put("parseFailureKey",new HashSet<String>());
	       }
	       if(!monitorDataMap.containsKey("testKey")){
	    	   monitorDataMap.put("testKey",new HashSet<String>());
	       }
	       
	     database.commit();
	    
	   }
	
	public List<String> getPreSubmitKey(){
		
		return commonGet(monitorDataMap.get("preSubmitKey"));
		
	}
	
	public void addPreSubmitKey(String key){
		commonAdd(key,"preSubmitKey");
	}
	
	public List<String> getSubmitKey(){
		
		return commonGet(monitorDataMap.get("submitKey"));
	}
	
	public void addSubmitKey(String key){
		commonAdd(key,"submitKey");
	}
	
	public List<String> getParseSuccessKey(){
		
		return commonGet(monitorDataMap.get("parseSuccessKey"));
	}
	
	public void addParseSuccessKey(String key){
		commonAdd(key,"parseSuccessKey");
	}
	
	public List<String> getParseFailureKey(){
		
		return commonGet(monitorDataMap.get("parseFailureKey"));
	}
	
	public void addParseFailureKey(String key){
		
		commonAdd(key,"parseFailureKey");
		
	}
	
    public List<String> getTestKey(){
		
		return commonGet(monitorDataMap.get("testKey"));
	}
	
	public void addTestKey(String key){
		
		commonAdd(key,"testKey");
		
	}
	
	public void removeSubmitKey(String key){
		HashSet<String> temp=monitorDataMap.get("submitKey");
		temp.remove(key);
		monitorDataMap.put("submitKey", temp);
		database.commit();
	}
	
	private void commonAdd(String key,String name){
		HashSet<String> temp=monitorDataMap.get(name);
		temp.add(key);
		monitorDataMap.put(name, temp);
		database.commit();
	}
	
	private List<String> commonGet(HashSet<String> keys){
		List<String>tempList=new ArrayList<String>();
	    Iterator<String> iterator=keys.iterator();
	    int i=0;
		while(iterator.hasNext()&&i<5){
			tempList.add(iterator.next());
			i++;
		}
		return tempList;
	}
	
	public void destory(){
		database.commit();
		database.compact();
		database.close();
	}

	public void setDatabase(DB database) {
		this.database = database;
	}  
}
